package project7.Storage;

import java.sql.*;

/**
 *
 * @author root
 */
public class DBConnect {

    private Connection connection = null;
    private Statement statement = null;
    private ResultSet rs = null;
    private String errorMessage = "";


    public DBConnect(){

    }

    public boolean defaultConnect(){
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            connection = DriverManager.getConnection("jdbc:mysql://urhere.boldlygoingnowhere.org/COP", "labrat", "m1k3");
        } catch (Exception e) {
            errorMessage += "connectToDatabase() error = " + e.getMessage();
            return false;
        }

        return true;
    }

     /**
     *  Open a connection to the Database;
     *
     *  Pass it the following database credentials
     *
     *      String url , String username, String password
     *
     *      It is assumed you are connecting to a jdbc:mysql database
     *
     */
    public boolean connectToDatabase(String host, String username, String password) {

        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            connection = DriverManager.getConnection("jdbc:mysql://" + host, username, password);
        } catch (Exception e) {
            errorMessage += "connectToDatabase() error = " + e.getMessage();
            return false;
        }

        return true;

    }

    /**
     *  Uses Open Connection connection.
     *
     *  Pass it the execution string and sql command type (boolean);
     *
     *      sql command type
     *          SELECT = TRUE;
     *          UPDATE, INSERT, DELETE = FALSE;
     *
     */
    public boolean executeQuery(String execString, boolean queryType) {
        try {
            statement = connection.createStatement();
            if (queryType) {
                rs = statement.executeQuery(execString);
            } else {
                statement.executeUpdate(execString);
            }
        } catch (Exception e) {
            errorMessage += "executeQuery() error = " + e.getMessage();
            return false;
        }
        return true;
    }

    /**
     *  Closes all the connections
     *      including:
     *          Connection connection;
     *          Statement statement;
     *          ResultSet rs;
     *
     */
    public boolean closeConnections(){
        try {
            if(rs!=null)
                rs.close();

            statement.close();
            connection.close();

            statement = null;
            rs = null;
            connection = null;

        } catch (Exception e) {
            errorMessage += "closeConnection() error = " + e.getMessage();
            return false;
        }
        return true;
    }



    /*------ Accessors and Mutators ------------------------------------------*/

    public Connection getConnection(){
        return connection;
    }
    public void setConnection(Connection con){
        connection = con;
    }

    public Statement getStatement(){
        return statement;
    }
    public void setStatement(Statement s){
        statement = s;
    }

    public ResultSet getResultSet(){
        return rs;
    }
    public void setResultSet(ResultSet r){
        rs = r;
    }

    public String getErrorMessage(){
        return errorMessage;
    }
    public void setErrorMessage(String error){
        errorMessage = error;
    }




    

}
